unit code_note;

{$mode ObjFPC}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, StdCtrls, ComCtrls, ExtCtrls, Menus,
  SynEdit, SynHighlighterPas, SynHighlighterVB, SynHighlighterBat,
  SynHighlighterCpp, SynHighlighterCss, SynHighlighterDiff, SynHighlighterHTML,
  SynHighlighterIni, SynHighlighterJava, SynHighlighterJScript,
  SynHighlighterPerl, SynHighlighterPHP, SynHighlighterPython,
  SynHighlighterSQL, SynHighlighterXML, SynHighlighterAny, SynPopupMenu, Types;

type

  { Tfrm_code_note }

  Tfrm_code_note = class(TFrame)
    cb_syn: TComboBox;
    ControlBar1: TControlBar;
    edit_title: TEdit;
    edit_text: TSynEdit;
    image_list_toolbar: TImageList;
    mn_syn_redo: TMenuItem;
    mn_syn_cut: TMenuItem;
    mn_syn_copy: TMenuItem;
    mn_syn_paste: TMenuItem;
    mn_syn_delete: TMenuItem;
    mn_syn_selectall: TMenuItem;
    Separator2: TMenuItem;
    Separator1: TMenuItem;
    mn_syn_undo: TMenuItem;
    mn_syn_select_all: TMenuItem;
    Separator9: TMenuItem;
    menu_synedit: TSynPopupMenu;
    syn_any: TSynAnySyn;
    syn_sql: TSynSQLSyn;
    syn_xml: TSynXMLSyn;
    syn_perl: TSynPerlSyn;
    syn_php: TSynPHPSyn;
    syn_python: TSynPythonSyn;
    syn_jscript: TSynJScriptSyn;
    syn_html: TSynHTMLSyn;
    syn_ini: TSynIniSyn;
    syn_java: TSynJavaSyn;
    syn_diff: TSynDiffSyn;
    syn_css: TSynCssSyn;
    syn_cpp: TSynCppSyn;
    syn_bat: TSynBatSyn;
    syn_freepascal: TSynFreePascalSyn;
    syn_pas: TSynPasSyn;
    syn_vb: TSynVBSyn;
    ToolBar1: TToolBar;
    tb_code_undo: TToolButton;
    tb_code_redo: TToolButton;
    ToolButton3: TToolButton;
    procedure cb_synChange(Sender: TObject);
    procedure cb_synSelect(Sender: TObject);
    procedure edit_textContextPopup(Sender: TObject; MousePos: TPoint;
        var Handled: Boolean);
    procedure mn_syn_copyClick(Sender: TObject);
    procedure mn_syn_cutClick(Sender: TObject);
    procedure mn_syn_deleteClick(Sender: TObject);
    procedure mn_syn_pasteClick(Sender: TObject);
    procedure mn_syn_redoClick(Sender: TObject);
    procedure mn_syn_select_allClick(Sender: TObject);
    procedure mn_syn_undoClick(Sender: TObject);
    procedure tb_code_redoClick(Sender: TObject);
    procedure tb_code_undoClick(Sender: TObject);
  private

  public
    function GetTitle(): string;
    function GetContent(): string;

    procedure SetTitle(title: string);
    procedure SetContent(content: string);
    procedure SetDefaultFont(font_name :string; font_size :Integer);
    procedure SetEditorSyn(syn_name: string);
  end;

implementation

{$R *.lfm}

{ Tfrm_code_note }

procedure Tfrm_code_note.cb_synSelect(Sender: TObject);
begin
  case cb_syn.Text of
    'Basic': edit_text.Highlighter := syn_vb;
    'BAT': edit_text.Highlighter := syn_bat;
    'C++': edit_text.Highlighter := syn_cpp;
    'CSS': edit_text.Highlighter := syn_css;
    'DIFF': edit_text.Highlighter := syn_diff;
    'FreePascal': edit_text.Highlighter := syn_freepascal;
    'HTML': edit_text.Highlighter := syn_html;
    'INI': edit_text.Highlighter := syn_ini;
    'Java': edit_text.Highlighter := syn_java;
    'JScript': edit_text.Highlighter := syn_jscript;
    'Pascal': edit_text.Highlighter := syn_pas;
    'Perl': edit_text.Highlighter := syn_perl;
    'PHP': edit_text.Highlighter := syn_php;
    'Python': edit_text.Highlighter := syn_python;
    'SQL': edit_text.Highlighter := syn_sql;
    'XML': edit_text.Highlighter := syn_xml;
    else
      edit_text.Highlighter := syn_any;
  end;
end;

procedure Tfrm_code_note.SetEditorSyn(syn_name: string);
begin
  case syn_name of
    'Basic': edit_text.Highlighter := syn_vb;
    'BAT': edit_text.Highlighter := syn_bat;
    'C++': edit_text.Highlighter := syn_cpp;
    'CSS': edit_text.Highlighter := syn_css;
    'Diff': edit_text.Highlighter := syn_diff;
    'FreePascal': edit_text.Highlighter := syn_freepascal;
    'HTML': edit_text.Highlighter := syn_html;
    'INI': edit_text.Highlighter := syn_ini;
    'Java': edit_text.Highlighter := syn_java;
    'JScript': edit_text.Highlighter := syn_jscript;
    'Pascal': edit_text.Highlighter := syn_pas;
    'Perl': edit_text.Highlighter := syn_perl;
    'PHP': edit_text.Highlighter := syn_php;
    'Python': edit_text.Highlighter := syn_python;
    'SQL': edit_text.Highlighter := syn_sql;
    'XML': edit_text.Highlighter := syn_xml;
    else
      edit_text.Highlighter := syn_any;
  end;
end;

procedure Tfrm_code_note.edit_textContextPopup(Sender: TObject;
    MousePos: TPoint; var Handled: Boolean);
var
  client_pos :TPoint;
begin
  client_pos := edit_text.ClientToScreen(MousePos);
  mn_syn_undo.Enabled:= edit_text.CanUndo;
  mn_syn_redo.Enabled:= edit_text.CanRedo;
  mn_syn_paste.Enabled:= edit_text.CanPaste;

  mn_syn_cut.Enabled:= Length(edit_text.SelText) > 0;
  mn_syn_copy.Enabled:= Length(edit_text.SelText) > 0;
  mn_syn_delete.Enabled:= Length(edit_text.SelText) > 0;

  menu_synedit.PopUp(client_pos.X, client_pos.Y);
end;

procedure Tfrm_code_note.mn_syn_copyClick(Sender: TObject);
begin
    edit_text.CopyToClipboard;
end;

procedure Tfrm_code_note.mn_syn_cutClick(Sender: TObject);
begin
    edit_text.CutToClipboard;
end;

procedure Tfrm_code_note.mn_syn_deleteClick(Sender: TObject);
begin
    edit_text.SelText:= '';
end;

procedure Tfrm_code_note.mn_syn_pasteClick(Sender: TObject);
begin
    edit_text.PasteFromClipboard(False);
end;

procedure Tfrm_code_note.mn_syn_redoClick(Sender: TObject);
begin
    if edit_text.CanRedo then
       edit_text.Redo;
end;

procedure Tfrm_code_note.mn_syn_select_allClick(Sender: TObject);
begin
    edit_text.SelectAll;
end;

procedure Tfrm_code_note.mn_syn_undoClick(Sender: TObject);
begin
    if edit_text.CanUndo then
       edit_text.Undo;
end;

procedure Tfrm_code_note.tb_code_redoClick(Sender: TObject);
begin
    edit_text.Redo;
end;

procedure Tfrm_code_note.tb_code_undoClick(Sender: TObject);
begin
    edit_text.Undo;
end;

procedure Tfrm_code_note.cb_synChange(Sender: TObject);
begin

end;

function Tfrm_code_note.GetTitle(): string;
begin
  Result := self.edit_title.Text;
end;

function Tfrm_code_note.GetContent(): string;
begin
  Result := self.edit_text.Text;
end;

procedure Tfrm_code_note.SetTitle(title: string);
begin
  self.edit_title.Text := title;
end;

procedure Tfrm_code_note.SetContent(content: string);
begin
  self.edit_text.Text := content;
end;

procedure Tfrm_code_note.SetDefaultFont(font_name :string; font_size :Integer);
begin
  self.edit_text.Font.Name:= font_name;
  self.edit_text.Font.Size:= font_size;
end;

end.
